﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using TouchRC.ListBoxes;
using TouchRC.Classes;
using TouchRC.Logic;
using TouchRC.BarManagers;
using TouchRC.Templates;
using TouchRC.Custom_Controls;
using TouchRC.Custom_Controls.Events;
using TouchRC.MetadataCaching;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace TouchRC
{
    public class MainMenuPanel : FluidPanelBase
    {
        public static MainMenuPanel GenerateMainMenuPanel(ComputerListBoxItem compy)
        {
            m_Computer = compy;
            MainMenuPanel panel = new MainMenuPanel();
            return panel;
        }

        #region Private Members

        private static ComputerListBoxItem m_Computer;
        private static MainMenuPanel instance;
        private MenuOptionCollection m_Options;
        private CachingStatusPanel m_MetadataProgressPanel = null;

        #region Fluid Controls

        private List<MenuOptionControl> m_MenuItems;
        private MenuOptionsPanel m_OptionsPanel;

        #endregion

        #endregion

        public static ComputerListBoxItem CurrentComputer { get { return m_Computer; } private set { m_Computer = value; } }

        public override void AddControls()
        {
            foreach (MenuOptionControl control in m_MenuItems)
            {
                Controls.Add(control);
            }
            Controls.Add(m_OptionsPanel);
        }

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {
            int listboxWidth = AppSettings.ScreenResolution.Width - ((int)(AppSettings.ScreenResolution.Width * 0.2));

            if (screenOrientation == ScreenOrientation.Landscape)
                listboxWidth = 100;

            int yPos = AppSettings.BaseControlHeight;
            int xPos = AppSettings.BaseControlHeight;

            int itemHeight = AppSettings.ButtonDimensions;
            
            int adjustment = AppSettings.CurrentOrientation == ScreenOrientation.Portrait ? 90 : 0;

            if (m_MetadataProgressPanel != null)
            {
                m_MetadataProgressPanel.Bounds = new Rectangle(0, 0, m_MetadataProgressPanel.ScaleX(AppSettings.ScreenResolution.Width), 0);
                m_MetadataProgressPanel.ReadjustHeight();
            }

            for (int i = 0; i < m_MenuItems.Count; i++)
            {
                MenuOptionControl control = m_MenuItems[i] as MenuOptionControl;
                control.Bounds = new Rectangle(control.ScaleX(xPos), control.ScaleY(yPos), 
                    control.ScaleX(listboxWidth), control.ScaleY(itemHeight));

                yPos += itemHeight;

                if (i == m_Options.SelectedIndex)
                {
                    // set the position of the MainMenuOptions
                    if (AppSettings.CurrentOrientation == ScreenOrientation.Landscape)
                    {
                        int x = 150;
                        int y = 50;
                        int width = 200;
                        int height = 170;

                        m_OptionsPanel.Bounds = new Rectangle(m_OptionsPanel.ScaleX(x), m_OptionsPanel.ScaleY(y),
                            m_OptionsPanel.ScaleX(width), m_OptionsPanel.ScaleY(height));

                        m_OptionsPanel.OriginalLeft = ScaleX(x);
                        m_OptionsPanel.OriginalTop = ScaleX(y);
                    }
                    else
                    {
                        if (m_Options[i].Options.Count > 2)
                        {
                            adjustment = adjustment * 2;
                        }

                        int xAdjustment = 11;
                        int yAdjustment = 10;

                        int panelX = m_OptionsPanel.ScaleX(xPos + xAdjustment);
                        int panelY = m_OptionsPanel.ScaleY(yPos - yAdjustment);
                        m_OptionsPanel.Bounds = new Rectangle(panelX, panelY,
                            m_OptionsPanel.ScaleX(listboxWidth), m_OptionsPanel.ScaleY(adjustment));

                        m_OptionsPanel.OriginalLeft = panelX;
                        m_OptionsPanel.OriginalTop = panelY;
                    }
                    m_OptionsPanel.SetPositions();

                    yPos += adjustment;

                    control.ForeColor = Color.White;
                }
                else
                {
                    control.ForeColor = Color.Gray;
                }
            }
        }

        public override void Back()
        {
            m_OptionsPanel.Close();
            Close();
        }
        
        private void DrawOptions(List<MainMenuOption> options)
        {
            m_OptionsPanel.Close(ShowTransition.FromRight);
            m_OptionsPanel = new MenuOptionsPanel(options);
            SetControlPositions(AppSettings.CurrentOrientation);
            m_OptionsPanel.Show(ShowTransition.FromRight);
        }

        void option_ItemClick(object sender, MenuOptionEventArgs e)
        {
            m_Options.SelectedIndex = e.SelectedItemIndex;
            DrawOptions(e.Options);
            Invalidate();
        }

        public override void InitPanelControls()
        {
            m_MenuItems = new List<MenuOptionControl>();

            for (int i = 0; i < m_Options.Count; i++)
            {
                MainMenuItem item = m_Options[i] as MainMenuItem;

                MenuOptionControl option = new MenuOptionControl(item);//.Title, xPos, yPos, listboxWidth, itemHeight);
                option.ItemClick += new EventHandler<MenuOptionEventArgs>(option_ItemClick);
                option.Index = i;

                m_MenuItems.Add(option);
            }

            m_OptionsPanel = new MenuOptionsPanel();
        }

        public override void RefreshValues()
        {
        }

        public void ShowAndSelectSelectedItem()
        {
            Show();
            DrawOptions(m_Options[m_Options.SelectedIndex].Options);
            Invalidate();
        }

        public void SelectFirstOption()
        {
            m_Options.SelectedIndex = 0;
            DrawOptions(m_Options[0].Options);
            Invalidate();
        }

        public override List<BarOption> GetBarOptions()
        {
            List<BarOption> options = new List<BarOption>();

            if (m_Computer!= null && m_Computer.MetadataCacheDir != null &&
                Directory.Exists(m_Computer.MetadataCacheDir.FullName))
            {
                BarOption getCache = new BarOption();
                getCache.Image = ImageFactory.GetBitmap(ImageTypes.Cogs);
                getCache.Click += new EventHandler<EventArgs>(getCache_Click);
                options.Add(getCache);
            }
            //BarOption addOption = new BarOption();
            //addOption.Image = ImageFactory.GetBitmap(ImageTypes.Plus);

            //options.Add(editComputers);
            //options.Add(addOption);

            return options;
        }

        void getCache_Click(object sender, EventArgs e)
        {
            base.HideOptionsBar();
            if (m_Computer != null)
            {
                m_MetadataProgressPanel = new CachingStatusPanel();
                m_MetadataProgressPanel.Bounds = new Rectangle(0, 0, m_MetadataProgressPanel.ScaleX(AppSettings.ScreenResolution.Width), 0);
                m_MetadataProgressPanel.MaxHeight = AppSettings.ScreenResolution.Height;
                m_MetadataProgressPanel.BaseHeight = AppSettings.BaseControlHeight;
                m_MetadataProgressPanel.CancelClick += new EventHandler(m_MetadataProgressPanel_CancelClick);
                m_MetadataProgressPanel.Show(ShowTransition.FromBottom);
                if (m_Computer.MetadataCacheDir != null &&
                    Directory.Exists(m_Computer.MetadataCacheDir.FullName))
                {
                    ThreadPool.QueueUserWorkItem(MyMedia);
                }
                else
                {
                    m_MetadataProgressPanel.Text = "Could not find path.";
                }
            }
        }

        void m_MetadataProgressPanel_CancelClick(object sender, EventArgs e)
        {
            if (m_MetadataProgressPanel != null)
            {
                m_MetadataProgressPanel.Close(ShowTransition.FromBottom);
            }
        }

        private void UpdateMetadataCacheStatus(string message)
        {
            if (m_MetadataProgressPanel != null)
            {
                m_MetadataProgressPanel.Text = message;

                int xPos = AppSettings.ScreenResolution.Height - m_MetadataProgressPanel.UnscaleY(m_MetadataProgressPanel.Height);

                m_MetadataProgressPanel.Top = m_MetadataProgressPanel.ScaleY(xPos);
            }
        }

        private void MyMedia(Object stateInfo)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                TouchRC.MetadataCaching.CachingHelper.ProgressReporter reporter = UpdateMetadataCacheStatus;
                CachingHelper helper = new CachingHelper(reporter);
                helper.CacheMedia(m_Computer.MetadataCacheDir);
            }
            catch { }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        protected override void InitControl()
        {
            HeaderText = m_Computer.Name;

            m_Options = new MenuOptionCollection();

            base.InitControl();
            instance = this;
            this.Bounds = AvailableScreen;

            base.ShowMenuButton = true;
        }

        public override void Focus()
        {
        }

        #region Public Accessors

        public static MainMenuPanel Instance
        {
            get
            {
                if (instance == null) throw new Exception("No instance has been instantiated.");
                return instance;
            }
        }

        #endregion

        
    }
}
